package it.univpm.diiga.memorysim.views;
 
import it.univpm.diiga.memorysim.data.DataPlugin;


import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.layout.GridData;

public class SequenceSimulation extends ViewPart {

	public static final String ID = "it.univpm.diiga.memorysim.views.SequenceSimulation"; // TODO Needs to be whatever is mentioned in plugin.xml
	private Composite top = null;
	private Label labelListEvent = null;
	private Table tableEvent = null;
	@SuppressWarnings("unused")
	private TableViewer tableViewer = null;
	private Button eventToSeq,eventToSeq1 = null;
	private Button delButton = null;
	private Table tableSeqSimulation = null;
	@SuppressWarnings("unused")
	private TableViewer tableViewer1 = null;
	private Label labelOperazioni = null;
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		GridData gridData1 = new GridData();
		gridData1.verticalSpan = 7;
		gridData1.verticalAlignment = GridData.CENTER;
		gridData1.horizontalIndent = 0;
		gridData1.heightHint = 216;
		gridData1.widthHint = 400;
		gridData1.horizontalAlignment = GridData.FILL;
		GridData gridData = new GridData();
		gridData.verticalSpan = 7;
		gridData.verticalAlignment = GridData.FILL;
		gridData.widthHint = 400;
		gridData.heightHint = 216;
		gridData.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.verticalSpacing = 5;
		gridLayout.horizontalSpacing = 5;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		labelListEvent = new Label(top, SWT.NONE);
		labelListEvent.setText("Lista degli eventi");
		@SuppressWarnings("unused")
		Label filler3 = new Label(top, SWT.NONE);
		labelOperazioni = new Label(top, SWT.NONE);
		labelOperazioni.setText("Operazioni");
		tableEvent = new Table(top, SWT.NONE|SWT.BORDER);
		tableEvent.setHeaderVisible(true);
		tableEvent.setLayoutData(gridData1);
		tableEvent.setLinesVisible(true);
		TableColumn column1 = new TableColumn (tableEvent, SWT.CENTER);
		TableColumn column2 = new TableColumn (tableEvent, SWT.CENTER);
		column1.setText ("Processo");
		  column2.setText ("Pagina");
		column1.setWidth(200);
		column2.setWidth(200);
		@SuppressWarnings("unused")
		Label filler7 = new Label(top, SWT.NONE);
		tableSeqSimulation = new Table(top, SWT.NONE|SWT.BORDER);
		tableSeqSimulation.setHeaderVisible(true);
		tableSeqSimulation.setLayoutData(gridData);
		tableSeqSimulation.setLinesVisible(true);
		TableColumn col1Proc = new TableColumn (tableSeqSimulation, SWT.CENTER);
		TableColumn col2Pag = new TableColumn (tableSeqSimulation, SWT.CENTER);
		TableColumn col3Type = new TableColumn (tableSeqSimulation, SWT.CENTER);
		col1Proc.setText ("Processo");
		 col2Pag.setText ("Pagina");
		 col3Type.setText ("Tipo");
		col2Pag.setWidth(185);
		col1Proc.setWidth(185);
		col3Type.setWidth(30);
		tableViewer1 = new TableViewer(tableSeqSimulation);
		@SuppressWarnings("unused")
		Label filler4 = new Label(top, SWT.NONE);
		@SuppressWarnings("unused")
		Label filler12 = new Label(top, SWT.NONE);
		eventToSeq = new Button(top, SWT.NONE);
		eventToSeq.setText("r ->");
		eventToSeq1 = new Button(top, SWT.NONE);
		eventToSeq1.setText("w ->");
		@SuppressWarnings("unused")
		Label filler75 = new Label(top, SWT.NONE);
		@SuppressWarnings("unused")
		Label filler8 = new Label(top, SWT.NONE);
		@SuppressWarnings("unused")
		Label filler711 = new Label(top, SWT.NONE);
		@SuppressWarnings("unused")
		Label filler715 = new Label(top, SWT.NONE);
		@SuppressWarnings("unused")
		Label filler716 = new Label(top, SWT.NONE);
		delButton = new Button(top, SWT.NONE);
		delButton.setText("Elimina Operazione");
		tableViewer = new TableViewer(tableEvent);
		tableEvent.addMouseListener(new MouseListener(){

			public void mouseDoubleClick(MouseEvent e) {
				TableItem[] itemEvent=tableEvent.getSelection();
				if(itemEvent.length>0){
				TableItem item1 = new TableItem (tableSeqSimulation, SWT.NONE);
				
				item1.setText (0,itemEvent[0].getText(0));
				item1.setText (1,itemEvent[0].getText(1));
				item1.setText (2,"r");
				}
				updateListRequest();
			}

			public void mouseDown(MouseEvent e) {}

			public void mouseUp(MouseEvent e) {}
			
		});
		eventToSeq1.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] itemEvent=tableEvent.getSelection();
				if(itemEvent.length>0){
				TableItem item1 = new TableItem (tableSeqSimulation, SWT.NONE);
				
				item1.setText (0,itemEvent[0].getText(0));
				item1.setText (1,itemEvent[0].getText(1));
				item1.setText (2,"w");
				}
				updateListRequest();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		eventToSeq.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] itemEvent=tableEvent.getSelection();
				if(itemEvent.length>0){
				TableItem item1 = new TableItem (tableSeqSimulation, SWT.NONE);
				
				item1.setText (0,itemEvent[0].getText(0));
				item1.setText (1,itemEvent[0].getText(1));
				item1.setText (2,"r");
				}
				updateListRequest();
			}
			
		});
		delButton.addSelectionListener(new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent e) {	}
			
			public void widgetSelected(SelectionEvent e) {
				tableSeqSimulation.remove(tableSeqSimulation.getSelectionIndices());
				updateListRequest();
			}
		});
		popolatePage();
		Load();
	}

	private void Load() {
		if(DataPlugin.listProcessRequest!=null){
			for(int i=0;i<DataPlugin.listProcessRequest.size();i++){
				TableItem item1 = new TableItem (tableSeqSimulation, SWT.NONE);		
				item1.setText (0,DataPlugin.listProcessRequest.get(i).getPID()+"");
				item1.setText (1,DataPlugin.listProcessRequest.get(i).getPagina()+"");
				item1.setText (2,DataPlugin.listProcessRequest.get(i).getType()+"");
			}
		}
	}

	private void popolatePage() {
			for(int i=0;i<DataPlugin.listProcess.size();i++){
				int n=DataPlugin.listProcess.get(i).getTableDim();
				for (int j=1;j<=n;j++){
					TableItem item1 = new TableItem (tableEvent, SWT.NONE);
					item1.setText (0,DataPlugin.listProcess.get(i).getPID()+"");
					item1.setText (1, j+"");
				}
			}
	}

	@Override
	public void setFocus() {

	}

	private void updateListRequest(){
		DataPlugin.newProcessRequest();
		for(int i=0;i<tableSeqSimulation.getItemCount();i++){
			TableItem itemEvent = tableSeqSimulation.getItem(i);
			int proc=Integer.parseInt(itemEvent.getText(0));
			int page=Integer.parseInt(itemEvent.getText(1));
			String type=itemEvent.getText(2);
			DataPlugin.addProcessRequest(proc, page,type);
		}
	}
} 
